home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / AppsToGo / DTS.Draw / TLineObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  8.9 KB  |  344 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        TLineObj.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* See the files "=How to write your app" and "=Using TreeObj.c" for information
  20. ** on this function. */
  21.  
  22. /* This file implements the messages for the round-rect object.  Many of the messages
  23. ** can be handled by the rect object, as they deal with a rect structure.  Only
  24. ** a few of them are round-rect-specific. */
  25.  
  26. /* It would seem that you would want a custom hit-test message handler here, but
  27. ** the rect object first checks to see if the hit is within the bounding box of
  28. ** the object, and if so, it then calls the object to return the region.  This
  29. ** allows the rect object to generically handle hit-testing. */
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  38. #include "App.protos.h"        /* Get the prototypes for the application.        */
  39.  
  40. #ifndef __OSEVENTS__
  41. #include <OSEvents.h>
  42. #endif
  43.  
  44. #ifndef __OSUTILS__
  45. #include <OSUtils.h>
  46. #endif
  47.  
  48. #ifndef __QUICKDRAW__
  49. #include <Quickdraw.h>
  50. #endif
  51.  
  52. #ifndef __TREEOBJ2__
  53. #include "TreeObj2.h"
  54. #endif
  55.  
  56. #ifndef __UTILITIES__
  57. #include "Utilities.h"
  58. #endif
  59.  
  60.  
  61.  
  62. static OSErr    LineLayerProc(LayerObj theLayer, short message);
  63.  
  64.  
  65.  
  66. /*****************************************************************************/
  67.  
  68.  
  69.  
  70. static LineObjPeek    *gMWERKSDebug;
  71.     /* For Metroweks debugging of AppsToGo "objects", you need an instance of the
  72.     ** same type you wish to view it in the debugger.  Now we have an instance. */
  73.  
  74.  
  75.  
  76. /*****************************************************************************/
  77. /*****************************************************************************/
  78.  
  79. #ifdef applec
  80. #pragma segment DTSDrawSeg2
  81. #endif
  82.  
  83. /*****************************************************************************/
  84. /*****************************************************************************/
  85.  
  86.  
  87.  
  88. long    TLineObj(TreeObjHndl hndl, short message, long data)
  89. {
  90.     Rect        rct, drct, grabber;
  91.     RgnHandle    rgn, accumRgn;
  92.     short        orn, h, w, i;
  93.     LayerObj    lineLayer;
  94.     Rect        *rptr;
  95.     Boolean        change;
  96.     ClickInfo    *click;
  97.     RGBColor    rgb, oldRgb;
  98. #if VH_VERSION
  99.     char        *cptr;
  100. #endif
  101.  
  102.     switch (message) {
  103.         case INITMESSAGE:
  104.         case FREEMESSAGE:
  105.         case COPYMESSAGE:
  106.         case UNDOMESSAGE:
  107.         case CONVERTMESSAGE:
  108.         case FREADMESSAGE:
  109.         case FWRITEMESSAGE:
  110.         case HREADMESSAGE:
  111.         case HWRITEMESSAGE:
  112.         case HITTESTMESSAGE:
  113.         case SETOBJRECTMESSAGE:
  114.         case SECTOBJRECTMESSAGE:
  115.         case CLICKMESSAGE:
  116.         case KEYMESSAGE:
  117.         case SETSELECTMESSAGE:
  118.         case GETSELECTMESSAGE:
  119.         case COMPAREMESSAGE:
  120.             return(TRectObj(hndl, message, data));
  121.             break;
  122.  
  123.         case GETBBOXMESSAGE:
  124.             TRectObj(hndl, message, data);
  125.             h = (mDerefCommon(hndl)->penHeight / 2) + 1;
  126.             w = (mDerefCommon(hndl)->penWidth  / 2) + 1;
  127.             rptr = (Rect *)data;
  128.             rptr->left   -= w;
  129.             rptr->right  += w;
  130.             rptr->top    -= h;
  131.             rptr->bottom += h;
  132.             break;
  133.  
  134.         case GETOBJRECTMESSAGE:
  135.             TRectObj(hndl, message, data);
  136.             rptr = (Rect *)data;
  137.             if (rptr->left == rptr->right ) rptr->right++;
  138.             if (rptr->top  == rptr->bottom) rptr->bottom++;
  139.             break;
  140.  
  141.         case GETRGNMESSAGE:
  142.             rgn      = NewRgn();
  143.             accumRgn = (RgnHandle)data;
  144.             if (accumRgn)
  145.                 if (GetHandleSize((Handle)accumRgn) > 10000)
  146.                     return((long)rgn);
  147.             NewLayer(&lineLayer, nil, LineLayerProc, nil, 0, (long)hndl);
  148.             if (lineLayer) {
  149.                 SetLayerWorld(lineLayer);
  150.                 TLineObj(hndl, DRAWMESSAGE, DRAWMASK);
  151.                 if (gQDVersion)
  152.                     BitMapToRegion(rgn, (BitMapPtr)(*((CGrafPtr)(*lineLayer)->layerPort)->portPixMap));
  153.                 else
  154.                     BitMapToRegion(rgn, &(*lineLayer)->layerPort->portBits);
  155.                 ResetLayerWorld(lineLayer);
  156.                 DisposeLayer(lineLayer);
  157.                 if (!data)
  158.                     InsetRgn(rgn, -1, -1);
  159.             }
  160.             if (accumRgn)
  161.                 UnionRgn(rgn, accumRgn, accumRgn);
  162.             return((long)rgn);
  163.             break;
  164.  
  165.         case DRAWMESSAGE:
  166.             rct  = mDerefLine(hndl)->rect;
  167.             orn  = mDerefLine(hndl)->flip;
  168.             drct = rct;
  169.             switch (orn) {
  170.                 case 1:
  171.                     drct.top    = rct.bottom;
  172.                     drct.bottom = rct.top;
  173.                     break;
  174.                 case 2:
  175.                     drct.left  = rct.right;
  176.                     drct.right = rct.left;
  177.                     break;
  178.                 case 3:
  179.                     drct.top    = rct.bottom;
  180.                     drct.bottom = rct.top;
  181.                     drct.left   = rct.right;
  182.                     drct.right  = rct.left;
  183.                     break;
  184.             }
  185.             h = mDerefCommon(hndl)->penHeight;
  186.             w = mDerefCommon(hndl)->penWidth;
  187.             PenSize(w, h);
  188.             switch (data) {
  189.                 case DRAWOBJ:
  190.                     if (gQDVersion) {
  191.                         GetForeColor(&oldRgb);
  192.                         rgb = mDerefLine(hndl)->borderColor;
  193.                         RGBForeColor(&rgb);
  194.                     }
  195.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  196.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  197.                     if (gQDVersion)
  198.                         RGBForeColor(&oldRgb);
  199.                     break;
  200.                 case ERASEOBJ:
  201.                     break;
  202.                 case DRAWSELECT:
  203.                     if (mDerefCommon(hndl)->selected) {
  204.                         for (i = 0; i < 2; ++i) {
  205.                             grabber.top  = i ? drct.top  : drct.bottom;
  206.                             grabber.left = i ? drct.left : drct.right;
  207.                             grabber.bottom = (grabber.top  -= 3) + 6;
  208.                             grabber.right  = (grabber.left -= 3) + 6;
  209.                             InvertRect(&grabber);
  210.                         }
  211.                     }
  212.                     break;
  213.                 case DRAWGHOST:
  214.                     PenMode(patXor);
  215.                     TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  216.                     break;
  217.                 case DRAWMASK:
  218.                     ForeColor(blackColor);
  219.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  220.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  221.                     break;
  222.             }
  223.             PenNormal();
  224.             break;
  225.  
  226.         case PRINTMESSAGE:
  227.             TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  228.             break;
  229.  
  230. #if VH_VERSION
  231.         case VHMESSAGE:
  232.             cptr = ((VHFormatDataPtr)data)->data;
  233.             ccatchr(cptr, 13, 2);
  234.             ccat   (cptr, "$10: TRectObj:");
  235.             ccatchr(cptr, 13, 1);
  236.             ccat   (cptr, "  $00: selected     = ");
  237.             ccatdec(cptr, mDerefLine(hndl)->selected);
  238.             ccatchr(cptr, 13, 1);
  239.             rct = mDerefLine(hndl)->rect;
  240.             ccat      (cptr, "  $02: rect         = ($");
  241.             ccatpadhex(cptr, 0, 4, 4, rct.top);
  242.             ccat      (cptr, ",$");
  243.             ccatpadhex(cptr, 0, 4, 4, rct.left);
  244.             ccat      (cptr, ",$");
  245.             ccatpadhex(cptr, 0, 4, 4, rct.bottom);
  246.             ccat      (cptr, ",$");
  247.             ccatpadhex(cptr, 0, 4, 4, rct.right);
  248.             ccat      (cptr, ")");
  249.             ccatchr   (cptr, 13, 1);
  250.             ccat      (cptr, "  $0A: penHeight    = ");
  251.             ccatdec   (cptr, mDerefLine(hndl)->penHeight);
  252.             ccatchr   (cptr, 13, 1);
  253.             ccat      (cptr, "  $0A: penWidth     = ");
  254.             ccatdec   (cptr, mDerefLine(hndl)->penWidth);
  255.             ccatchr   (cptr, 13, 1);
  256.             ccat      (cptr, "  $0E: borderColor  = ($");
  257.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.red);
  258.             ccat      (cptr, ",$");
  259.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.green);
  260.             ccat      (cptr, ",$");
  261.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.blue);
  262.             ccat      (cptr, ")");
  263.             ccatchr   (cptr, 13, 1);
  264.             ccat      (cptr, "  $14: content      = ($");
  265.             ccatdec   (cptr, mDerefLine(hndl)->content);
  266.             ccatchr   (cptr, 13, 1);
  267.             ccat      (cptr, "  $16: contentColor = ($");
  268.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.red);
  269.             ccat      (cptr, ",$");
  270.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.green);
  271.             ccat      (cptr, ",$");
  272.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.blue);
  273.             ccat      (cptr, ")");
  274.             ccatchr   (cptr, 13, 1);
  275.             ccat      (cptr, "  $1A: flip         = ($");
  276.             ccatdec   (cptr, mDerefLine(hndl)->flip);
  277.             return(true);
  278.             break;
  279. #endif
  280.  
  281.         case SIZEMESSAGE:
  282.             change = TRectObj(hndl, message, data);
  283.             if (change) {
  284.                 click = (ClickInfo *)data;
  285.                 mDerefLine(hndl)->flip = click->newFlip;
  286.             }
  287.             return(change);
  288.             break;
  289.  
  290.         default:
  291.             break;
  292.     }
  293.  
  294.     return(noErr);
  295. }
  296.  
  297.  
  298.  
  299. /*****************************************************************************/
  300.  
  301.  
  302.  
  303. static OSErr    LineLayerProc(LayerObj theLayer, short message)
  304. {
  305.     OSErr        err;
  306.     TreeObjHndl    line;
  307.     Rect        rct;
  308.     CGrafPtr    keepPort;
  309.     GDHandle    keepGDevice;
  310.     GWorldPtr    layerWorld;
  311.  
  312.     switch (message) {
  313.         case kLayerInit:
  314.             err = noErr;
  315.             if (theLayer) {
  316.                 if (!(*theLayer)->layerPort) {
  317.                     line = (TreeObjHndl)(*theLayer)->layerData;
  318.                     TLineObj(line, GETBBOXMESSAGE, (long)&rct);
  319.                     GetGWorld(&keepPort, &keepGDevice);        /* Keep the GWorld. */
  320.                     err = NewGWorld(&layerWorld, 1, &rct, nil, nil, 0);
  321.                     if (err == noErr) {
  322.                         (*theLayer)->layerOwnsPort = true;
  323.                         SetPort((*theLayer)->layerPort = (GrafPtr)layerWorld);
  324.                         SetOrigin(rct.left, rct.top);
  325.                         EraseRect(&rct);
  326.                     }
  327.                     SetGWorld(keepPort, keepGDevice);        /* Restore the kept GWorld. */
  328.                 }
  329.             }
  330.             else err = paramErr;
  331.             break;
  332.  
  333.         default:
  334.             err = DefaultLayerProc(theLayer, message);
  335.                 /* Default behavior for everything else. */
  336.             break;
  337.     }
  338.  
  339.     return(err);
  340. }
  341.  
  342.  
  343.  
  344.